package com.gf.user.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gf.base.controller.BasicController;
import com.gf.base.type.LicenseState;
import com.gf.base.type.Role;
import com.gf.common.constant.Constants;
import com.gf.common.util.MailHelper;
import com.gf.common.util.ReloadUserInfoHelper;
import com.gf.common.util.WebHelper;
import com.gf.message.service.MessageService;
import com.gf.user.model.User;
import com.gf.user.service.AdminService;
import com.gf.user.service.UserBaseService;
import com.gf.user.service.UserService;

/**
 * 管理员的操作<br>
 * 
 * @author zhuguifeng
 * 
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BasicController {

	@Autowired
	private UserBaseService userBaseService;

	@Autowired
	private UserService userService;

	@Autowired
	private AdminService adminService;

	@Autowired
	private MessageService messageService;

	private static Logger logger = LoggerFactory
			.getLogger(AdminController.class);

	/**
	 * 系统客服邮箱
	 */
	@Value("{system.service.mail}")
	private String serviceMail;

	/**
	 * 医生上传的证件通过管理员审核
	 * 
	 * @param userId
	 */
	@RequestMapping(value = "/passChecking", method = RequestMethod.POST)
	public @ResponseBody
	boolean passChecking(long userId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return false;
		}
		if (user.getRole().toString().equals(Role.ADMIN.toString())
				|| user.getRole().toString().equals(Role.SUPER.toString())) {
			boolean result = userBaseService.passCheckingOfDoctor(userId,
					LicenseState.PASSED);
			if (result) {
				ReloadUserInfoHelper.setNeedReload(userId);
				messageService
						.sendMessageByOne(user.getId(), "系统消息", userId, "系统通知",
								"很高兴地通知您，您上传的医生执照已通过审核！<br>现在你可以发布测试项目！赶紧去体验吧！");
			}
			return result;
		}

		return false;
	}

	/**
	 * 拒绝
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/deny", method = RequestMethod.POST)
	public @ResponseBody
	boolean deny(long userId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return false;
		}
		if (user.getRole().toString().equals(Role.ADMIN.toString())
				|| user.getRole().toString().equals(Role.SUPER.toString())) {
			boolean result = userBaseService.passCheckingOfDoctor(userId,
					LicenseState.DENY);
			if (result) {
				ReloadUserInfoHelper.setNeedReload(userId);
				// 发送站内信
				messageService.sendMessageByOne(user.getId(), "系统消息", userId,
						"系统通知", "很遗憾地通知您，您上传的医生执照不符合要求，请到'设置'里重新上传！");

			}
			return result;
		}

		return false;

	}

	/**
	 * 冻结某用户
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/freeze", method = RequestMethod.POST)
	public @ResponseBody
	boolean freezeUser(long userId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return false;
		}
		if (user.getRole().toString().equals(Role.ADMIN.toString())
				|| user.getRole().toString().equals(Role.SUPER.toString())) {
			boolean result = adminService.freezeUser(userId);
			if (result) {
				ReloadUserInfoHelper.setNeedReload(userId);
				try {
					User freezedUser = userService.getUserById(userId);
					String content = "亲爱的用户，由于您一些不正当地操作，为了维护网站的正常运营<br>"
							+ "很遗憾地告诉你，我们对您的帐户进行了冻结！<br>" + "您可以以邮件的形式反馈到以下邮箱："
							+ serviceMail;
					MailHelper.sendHtmlMail(freezedUser.getEmail(),
							"系统通知--用户冻结", content);

				} catch (Exception e) {
					logger.error(e.getMessage());
				}
			}
			return result;
		}

		return false;
	}

	/**
	 * 解冻某用户
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/release", method = RequestMethod.POST)
	public @ResponseBody
	boolean thowUser(long userId) {

		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return false;
		}
		if (user.getRole().toString().equals(Role.ADMIN.toString())
				|| user.getRole().toString().equals(Role.SUPER.toString())) {
			boolean result = adminService.thawUser(userId);
			if (result) {
				ReloadUserInfoHelper.setNeedReload(userId);
				try {
					User releaseUser = userService.getUserById(userId);
					String content = "亲爱的用户，很高兴地通知您，你的帐号已解冻<br>"
							+ "希望你能合法地利用该网站资源，祝好！";
					//发送站内信
					messageService.sendMessageByOne(user.getId(), "医务系统",
							userId, "系统通知--用户解冻", content);
					MailHelper.sendHtmlMail(releaseUser.getEmail(),
							"系统通知--用户解冻", content);
				} catch (Exception e) {
					logger.error(e.getMessage());
				}
			}
			return result;
		}

		return false;
	}

}
